home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch3 / PalDraw.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-04-01  |  12.8 KB  |  407 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
  3. Begin VB.Form PalDrawForm 
  4.    Caption         =   "PalDraw"
  5.    ClientHeight    =   4260
  6.    ClientLeft      =   1455
  7.    ClientTop       =   1440
  8.    ClientWidth     =   7200
  9.    DrawMode        =   14  'Copy Pen
  10.    LinkTopic       =   "Form1"
  11.    PaletteMode     =   1  'UseZOrder
  12.    ScaleHeight     =   4260
  13.    ScaleWidth      =   7200
  14.    Begin VB.CommandButton cmdClear 
  15.       Caption         =   "Clear"
  16.       Height          =   495
  17.       Left            =   480
  18.       TabIndex        =   13
  19.       Top             =   2640
  20.       Width           =   1215
  21.    End
  22.    Begin VB.PictureBox picCanvas 
  23.       AutoRedraw      =   -1  'True
  24.       Height          =   4238
  25.       Left            =   2700
  26.       ScaleHeight     =   4185
  27.       ScaleWidth      =   4440
  28.       TabIndex        =   0
  29.       Top             =   0
  30.       Width           =   4500
  31.    End
  32.    Begin VB.PictureBox picForeColor 
  33.       AutoRedraw      =   -1  'True
  34.       Height          =   500
  35.       Left            =   840
  36.       ScaleHeight     =   435
  37.       ScaleWidth      =   435
  38.       TabIndex        =   12
  39.       Top             =   1440
  40.       Width           =   500
  41.    End
  42.    Begin VB.PictureBox picFillColor 
  43.       AutoRedraw      =   -1  'True
  44.       Height          =   500
  45.       Left            =   840
  46.       ScaleHeight     =   435
  47.       ScaleWidth      =   435
  48.       TabIndex        =   9
  49.       Top             =   2040
  50.       Width           =   500
  51.    End
  52.    Begin VB.ComboBox cboFill 
  53.       Height          =   315
  54.       ItemData        =   "PalDraw.frx":0000
  55.       Left            =   840
  56.       List            =   "PalDraw.frx":001C
  57.       Style           =   2  'Dropdown List
  58.       TabIndex        =   8
  59.       Top             =   1080
  60.       Width           =   1815
  61.    End
  62.    Begin VB.ComboBox cboDraw 
  63.       Height          =   315
  64.       ItemData        =   "PalDraw.frx":008F
  65.       Left            =   840
  66.       List            =   "PalDraw.frx":00A8
  67.       Style           =   2  'Dropdown List
  68.       TabIndex        =   6
  69.       Top             =   720
  70.       Width           =   1815
  71.    End
  72.    Begin VB.ComboBox cboObject 
  73.       Height          =   315
  74.       ItemData        =   "PalDraw.frx":00E7
  75.       Left            =   840
  76.       List            =   "PalDraw.frx":00F7
  77.       Style           =   2  'Dropdown List
  78.       TabIndex        =   3
  79.       Top             =   0
  80.       Width           =   1815
  81.    End
  82.    Begin VB.TextBox txtWidth 
  83.       Height          =   285
  84.       Left            =   840
  85.       MaxLength       =   1
  86.       TabIndex        =   2
  87.       Text            =   "1"
  88.       Top             =   360
  89.       Width           =   375
  90.    End
  91.    Begin MSComDlg.CommonDialog FileDialog 
  92.       Left            =   1560
  93.       Top             =   1560
  94.       _ExtentX        =   847
  95.       _ExtentY        =   847
  96.       _Version        =   393216
  97.       CancelError     =   -1  'True
  98.    End
  99.    Begin VB.Label Label1 
  100.       Caption         =   "FillColor"
  101.       Height          =   255
  102.       Index           =   5
  103.       Left            =   0
  104.       TabIndex        =   11
  105.       Top             =   2160
  106.       Width           =   855
  107.    End
  108.    Begin VB.Label Label1 
  109.       Caption         =   "ForeColor"
  110.       Height          =   255
  111.       Index           =   4
  112.       Left            =   0
  113.       TabIndex        =   10
  114.       Top             =   1560
  115.       Width           =   855
  116.    End
  117.    Begin VB.Label Label1 
  118.       Caption         =   "FillStyle"
  119.       Height          =   255
  120.       Index           =   3
  121.       Left            =   0
  122.       TabIndex        =   7
  123.       Top             =   1080
  124.       Width           =   855
  125.    End
  126.    Begin VB.Label Label1 
  127.       Caption         =   "DrawStyle"
  128.       Height          =   255
  129.       Index           =   2
  130.       Left            =   0
  131.       TabIndex        =   5
  132.       Top             =   720
  133.       Width           =   855
  134.    End
  135.    Begin VB.Label Label1 
  136.       Caption         =   "DrawWidth"
  137.       Height          =   255
  138.       Index           =   1
  139.       Left            =   0
  140.       TabIndex        =   4
  141.       Top             =   360
  142.       Width           =   855
  143.    End
  144.    Begin VB.Label Label1 
  145.       Caption         =   "Object"
  146.       Height          =   255
  147.       Index           =   0
  148.       Left            =   0
  149.       TabIndex        =   1
  150.       Top             =   0
  151.       Width           =   855
  152.    End
  153.    Begin VB.Menu mnuFile 
  154.       Caption         =   "&File"
  155.       Begin VB.Menu mnuFileOpen 
  156.          Caption         =   "&Open..."
  157.          Shortcut        =   ^O
  158.       End
  159.    End
  160. Attribute VB_Name = "PalDrawForm"
  161. Attribute VB_GlobalNameSpace = False
  162. Attribute VB_Creatable = False
  163. Attribute VB_PredeclaredId = True
  164. Attribute VB_Exposed = False
  165. Option Explicit
  166. Private Const CLOSEST_IN_PALETTE = &H2000000
  167. ' The object types.
  168. Private Enum ObjectTypes
  169.     objLine
  170.     objBox
  171.     objEllipse
  172.     objPoint
  173. End Enum
  174. ' The type of object we should draw.
  175. Private SelectedObject As ObjectTypes
  176. Private Rubberbanding As Boolean
  177. Private OldStyle As Integer
  178. Private FirstX As Single
  179. Private FirstY As Single
  180. Private LastX As Single
  181. Private LastY As Single
  182. Private Declare Function RealizePalette Lib "gdi32" (ByVal hdc As Long) As Long
  183. ' Draw the object.
  184. Private Sub DrawObject()
  185.     ' Draw the object.
  186.     Select Case SelectedObject
  187.         Case objLine
  188.             picCanvas.Line (FirstX, FirstY)-(LastX, LastY)
  189.         Case objBox
  190.             picCanvas.Line (FirstX, FirstY)-(LastX, LastY), , B
  191.         Case objEllipse
  192.             DrawEllipse FirstX, FirstY, LastX, LastY
  193.         Case objPoint
  194.             picCanvas.PSet (LastX, LastY)
  195.     End Select
  196. End Sub
  197. ' Draw an ellipse specified by a bounding box.
  198. Private Sub DrawEllipse( _
  199.     ByVal xmin As Single, ByVal ymin As Single, _
  200.     ByVal xmax As Single, ByVal ymax As Single)
  201. Dim cx As Single
  202. Dim cy As Single
  203. Dim wid As Single
  204. Dim hgt As Single
  205. Dim aspect As Single
  206. Dim radius As Single
  207.     ' Find the center.
  208.     cx = (xmin + xmax) / 2
  209.     cy = (ymin + ymax) / 2
  210.     ' Get the ellipse's size.
  211.     wid = xmax - xmin
  212.     hgt = ymax - ymin
  213.     If wid = 0 Or hgt = 0 Then Exit Sub
  214.     aspect = hgt / wid
  215.     ' See which dimension is larger and
  216.     ' calculate the radius.
  217.     If wid > hgt Then
  218.         ' The major axis is horizontal.
  219.         ' Use a horizontal radius.
  220.         radius = wid / 2
  221.     Else
  222.         ' The major axis is vertical.
  223.         ' Use a vertical radius.
  224.         radius = aspect * wid / 2
  225.     End If
  226.     ' Draw the circle.
  227.     picCanvas.Circle (cx, cy), radius, , , , aspect
  228. End Sub
  229. ' Erase the image using the current fill color.
  230. Private Sub cmdClear_Click()
  231.     picCanvas.Line (0, 0)-(picCanvas.ScaleWidth, picCanvas.ScaleHeight), picCanvas.FillColor, BF
  232. End Sub
  233. ' Start a rubberbanding of some sort.
  234. Private Sub picCanvas_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  235.     ' Let MouseMove know we are rubberbanding.
  236.     Rubberbanding = True
  237.     ' Save values so we can restore them later.
  238.     OldStyle = picCanvas.DrawStyle
  239.     picCanvas.DrawMode = vbInvert
  240.     If SelectedObject = objLine Then
  241.         picCanvas.DrawStyle = vbSolid
  242.     Else
  243.         picCanvas.DrawStyle = vbDot
  244.     End If
  245.     ' Save the starting coordinates.
  246.     FirstX = X
  247.     FirstY = Y
  248.     ' Save the ending coordinates.
  249.     LastX = X
  250.     LastY = Y
  251.     ' Draw the appropriate rubberband object.
  252.     DrawObject
  253. End Sub
  254. ' Continue rubberbanding.
  255. Private Sub picCanvas_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  256.     ' If we are not rubberbanding, do nothing.
  257.     If Not Rubberbanding Then Exit Sub
  258.     ' Erase the previous rubberband object.
  259.     DrawObject
  260.     ' Save the new ending coordinates.
  261.     LastX = X
  262.     LastY = Y
  263.     ' Draw the new rubberband object.
  264.     DrawObject
  265. End Sub
  266. ' Finish rubberbanding and draw the object.
  267. Private Sub picCanvas_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  268.     ' If we are not rubberbanding, do nothing.
  269.     If Not Rubberbanding Then Exit Sub
  270.     ' We are no longer rubberbanding.
  271.     Rubberbanding = False
  272.     ' Erase the previous rubberband object.
  273.     DrawObject
  274.     ' Restore the original DrawMode and DrawStyle.
  275.     picCanvas.DrawMode = vbCopyPen
  276.     picCanvas.DrawStyle = OldStyle
  277.     ' Draw the final object.
  278.     DrawObject
  279. End Sub
  280. ' Select the draw style.
  281. Private Sub cboDraw_Click()
  282.     picCanvas.DrawStyle = cboDraw.ListIndex
  283. End Sub
  284. ' Select the fill style.
  285. Private Sub cboFill_Click()
  286.     picCanvas.FillStyle = cboFill.ListIndex
  287. End Sub
  288. ' Allow the user to select a new foreground color.
  289. Private Sub picForeColor_Click()
  290. Dim popup As New PalettePopup
  291. Dim clr As Long
  292.     ' Load the picture to get its palette.
  293.     popup.Picture = picCanvas.Picture
  294.     ' Fill the popup with palette colors.
  295.     popup.Fill
  296.         
  297.     ' Select the current foreground color.
  298.     popup.SelectedColor = picCanvas.ForeColor
  299.     ' Let the user select a color.
  300.     popup.Show vbModal
  301.     ' Set the selected color using the palete
  302.     ' relative RGB value.
  303.     clr = popup.SelectedColor + CLOSEST_IN_PALETTE
  304.     picCanvas.ForeColor = clr
  305.     picForeColor.Line (0, 0)-(picForeColor.ScaleWidth, picForeColor.ScaleHeight), clr, BF
  306.     Unload popup
  307. End Sub
  308. ' Allow the user to select a new fill color.
  309. Private Sub picFillColor_Click()
  310. Dim popup As New PalettePopup
  311. Dim clr As Long
  312.     ' Load the picture to get its palette.
  313.     popup.Picture = picCanvas.Picture
  314.     ' Fill the popup with palette colors.
  315.     popup.Fill
  316.         
  317.     ' Select the current background color.
  318.     popup.SelectedColor = picCanvas.FillColor
  319.     ' Let the user select a color.
  320.     popup.Show vbModal
  321.     ' Set the selected color using the palete
  322.     ' relative RGB value.
  323.     clr = popup.SelectedColor + CLOSEST_IN_PALETTE
  324.     picCanvas.FillColor = clr
  325.     picFillColor.Line (0, 0)-(picFillColor.ScaleWidth, picFillColor.ScaleHeight), clr, BF
  326.     Unload popup
  327. End Sub
  328. Private Sub Form_Load()
  329.     ' Select the default options.
  330.     cboDraw.ListIndex = picCanvas.DrawStyle
  331.     cboFill.ListIndex = picCanvas.FillStyle
  332.     cboObject.ListIndex = picCanvas.FillStyle
  333.     txtWidth.Text = Format$(picCanvas.DrawWidth)
  334.     ' Fill the color swatches.
  335.     ResetSwatches
  336. End Sub
  337. ' Set the colors in the swatches.
  338. Private Sub ResetSwatches()
  339. Dim clr As Long
  340.     picCanvas.Refresh
  341.     ' Make the swatches use the same logical
  342.     ' palette as the picCanvas.
  343.     picForeColor.Picture = picCanvas.Picture
  344.     picFillColor.Picture = picCanvas.Picture
  345.     ' Start with black again.
  346.     picCanvas.ForeColor = vbBlack
  347.     picCanvas.FillColor = vbBlack
  348.     picForeColor.Line (0, 0)-(picForeColor.ScaleWidth, picForeColor.ScaleHeight), vbBlack, BF
  349.     picFillColor.Line (0, 0)-(picFillColor.ScaleWidth, picFillColor.ScaleHeight), vbBlack, BF
  350. End Sub
  351. ' Make the controls as larger as possible.
  352. Private Sub Form_Resize()
  353. Dim wid As Single
  354.     wid = ScaleWidth - cboObject.Left - cboObject.Width - 30
  355.     If wid < 100 Then wid = 100
  356.     picCanvas.Move ScaleWidth - wid, 0, wid, ScaleHeight
  357. End Sub
  358. Private Sub mnuFileOpen_Click()
  359. Dim fname As String
  360.     ' Allow the user to pick a file.
  361.     On Error Resume Next
  362.     FileDialog.FileName = "*.BMP;*.ICO;*.DIB;*.JPG;*.GIF"
  363.     FileDialog.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  364.     FileDialog.ShowOpen
  365.     If Err.Number = cdlCancel Then
  366.         Exit Sub
  367.     ElseIf Err.Number <> 0 Then
  368.         Beep
  369.         MsgBox "Error selecting file.", , vbExclamation
  370.         Exit Sub
  371.     End If
  372.     On Error GoTo LoadError
  373.     fname = Trim$(FileDialog.FileName)
  374.     FileDialog.InitDir = Left$(fname, Len(fname) _
  375.         - Len(FileDialog.FileTitle) - 1)
  376.     Caption = "PalDraw [" & fname & "]"
  377.     ' Load the picture.
  378.     picCanvas.Picture = LoadPicture(fname)
  379.     RealizePalette picCanvas.hdc
  380.     ResetSwatches
  381.     Exit Sub
  382. LoadError:
  383.     Beep
  384.     MsgBox "Error loading picture " & fname & _
  385.         "." & vbCrLf & Error$, vbExclamation
  386. End Sub
  387. Private Sub cboObject_Click()
  388.     SelectedObject = cboObject.ListIndex
  389. End Sub
  390. ' Change set DrawWidth.
  391. Private Sub txtWidth_Change()
  392. Dim wid As Integer
  393.     If Not IsNumeric(txtWidth.Text) Then Exit Sub
  394.     wid = CInt(txtWidth.Text)
  395.     If wid < 1 Then Exit Sub
  396.     picCanvas.DrawWidth = wid
  397. End Sub
  398. ' Only allow 1 through 9.
  399. Private Sub txtWidth_KeyPress(KeyAscii As Integer)
  400.     If KeyAscii < Asc(" ") Or _
  401.        KeyAscii > Asc("~") Then Exit Sub
  402.     If KeyAscii >= Asc("1") And _
  403.        KeyAscii <= Asc("9") Then Exit Sub
  404.     Beep
  405.     KeyAscii = 0
  406. End Sub
  407.